package com.kingtsoft.study.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kingtsoft.study.common.ContextHolder;
import com.kingtsoft.study.constant.ParamConstants;
import com.kingtsoft.study.model.StudyHomework;
import com.kingtsoft.study.persist.StudyHomeworkMapper;
import com.kingtsoft.study.service.StudyHomeworkService;

/**
 * Title:
 * Description:
 * Copyright: Copyright (c) 2017-03-30 下午3:01
 * Company: wondersgroup.com
 *
 * @author jerry.pan(潘健雷)
 * @version 1.0
 */
@Service
public class StudyHomeworkServiceImpl implements StudyHomeworkService {

    @Autowired
    StudyHomeworkMapper homeworkMapper;

    /**
     * 获取作业
     *
     * @param id 作业主键
     * @return 作业
     * @author jerry.pan
     * @date 2017-03-30 15:08:11
     */
    @Override
    public StudyHomework load(String id){
        return homeworkMapper.selectByPrimaryKey(id);
    }

    /**
     * 分页获取老师布置的作业的列表
     *
     * @return pageInfo
     * @author jerry.pan
     * @date 2017-03-30 15:08:11
     */
    @Override
    public PageInfo<StudyHomework> listPage(PageInfo page, StudyHomework studyHomework) {
        //分页查询,紧跟着的第一个方法会被分页
        PageHelper.startPage(page);
        List<StudyHomework> list = homeworkMapper.listAll(studyHomework);
        //用PageInfo对结果进行包装
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }

    /**
     * 获取老师布置的作业的列表
     *
     * @return list
     * @author jerry.pan
     * @date 2017-03-30 15:08:11
     */
    @Override
    public List<StudyHomework> listAll(StudyHomework studyHomework) {
        return homeworkMapper.listAll(studyHomework);
    }


    /**
     *
     * @Title: addHomework
     * @Description: 老师发布作业，保存homework
     * @author: shenlx
     * @param studyHomework
     * @return: void
     */
	@Override
	public void addHomework(StudyHomework studyHomework) {

		studyHomework.setUserId(ContextHolder.getSessionUser().getId());
		studyHomework.setTeacherId(ContextHolder.getSessionTeacher().getId());
		studyHomework.setPublishDate(new Date());
		studyHomework.setPublishStatus(ParamConstants.PUBLISHSTATUS_1);
		homeworkMapper.insert(studyHomework);

	}

}
